package controller;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.entity.User;

public class CUtil {
	public static boolean checkLogIn(HttpServletRequest request, HttpServletResponse response){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			redirecter(request, response, "SLogin");
			return false;
		}
		return true;
	}
	
	public static void redirecter(HttpServletRequest request,
			HttpServletResponse response, String location) {
		try {
			RequestDispatcher disp = request.getRequestDispatcher(location);
			disp.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void lastPageRedirect(HttpServletRequest request,
			HttpServletResponse response){
		if(request.getAttribute("lastLocation")==null){
			redirecter(request, response, "SHome");
		}else{
			redirecter(request, response, (String)request.getAttribute("lastLocation"));
		}
	}
	
	public static String intToGender(int gender){
		if(gender==0){
			return "M";
		}else if(gender==1){
			return "F";
		}else{
			return "O";
		}
	}
	
	public static int genderToInt(String gender){
		if(gender.equals("M")){
			return 0;
		}else if(gender.equals("F")){
			return 1;
		}else{
			//O
			return 2;
		}
	}
	
	public static void setEncoding(HttpServletRequest request, HttpServletResponse response){
		response.setContentType("text/html; charset=UTF-8");
		try {
			request.setCharacterEncoding(response.getCharacterEncoding());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendMail(String titlep,String messageTextp,String addressp){
		final String title=titlep;
		final String messageText=messageTextp;
		final String address=addressp;
		Thread smt=new Thread(new Runnable() {
			@Override
			public void run() {
				final String username = "epicsmbs2@gmail.com";
				final String password = "mbs2epics";
		 
				Properties props = new Properties();
				props.put("mail.smtp.auth", "true");
				props.put("mail.smtp.starttls.enable", "true");
				props.put("mail.smtp.host", "smtp.gmail.com");
				props.put("mail.smtp.port", "587");
		 
				Session session = Session.getInstance(props,
				  new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				  });
		 
				try {
		 
					Message message = new MimeMessage(session);
					message.setFrom(new InternetAddress(username));
					message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(address));
					message.setSubject(title);
					message.setText(messageText);
		 
					Transport.send(message);
		 					
				} catch (MessagingException e) {
					e.printStackTrace();
				}
			}
		});
		smt.run();
		
	}
}
